package com.mskj.p3.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.mskj.p3.dao.UserDao;
import com.mskj.p3.model.Hobby;
import com.mskj.p3.model.User;
import com.mskj.p3.util.JdbcUtil;

@Repository
public class UserDaoImp implements UserDao {
	@Resource
	private JdbcTemplate jt;

	@Override
	public User login(User user) {
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("select u_id,upsd,uname,pid,grade from tbl_user where uname=? and upsd=?");
			pst.setString(1, user.getUname());
			pst.setString(2, user.getUpsd());		
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				User u =new User();
				u.setGrade(rs.getString("grade"));
				u.setUid(rs.getString("u_id"));
				u.setPid(rs.getString("pid"));
				u.setUname(rs.getString("uname"));
				u.setUpsd(rs.getString("upsd"));			
				return u;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.closeCon(con, pst);
		}
		return null;
	}

	@Override
	public User reigist(User user,Hobby hy) {
		    
		Connection con = null;
		PreparedStatement pst = null;
		PreparedStatement pst1 = null;
		System.out.println("进入测试程序里面！");
		try {
			con = JdbcUtil.getCon();
			pst = con.prepareStatement("insert into  tbl_user(u_id,uname,upsd) values(seq_user.nextval,?,?)");
			pst.setString(1, user.getUname());
			pst.setString(2, user.getUpsd());
	        if(pst.executeUpdate()!=0){
	        	UserDaoImp udi=new UserDaoImp();
	        	User u1=udi.login(user);
					
					
					
			pst1 = con.prepareStatement("insert into tbl_choose values(?,?)");
			pst1.setString(1, hy.getHid());
			pst1.setString(2, u1.getUid());
			
			System.out.println("hid="+hy.getHid());
			System.out.println("u_id="+user.getUid());
			System.out.println("u1_id="+u1.getUid());
			pst1.executeUpdate();
			System.out.println("ok");
		
			return user;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.closeCon(con, pst);
		}
		return null;
		
//		
//		String sql = "insert into tbl_user(uid,uname,upsd) values(seq_user.nextval,?,?,?)";
//		Object[] params = new Object[] { user.getUname(), user.getUpsd() };
//		int updateRecordNum = jt.update(sql, params);
//		if (updateRecordNum == 1)
//			return user;
//		return null;
	}

	public User displayUser(User user) {
		Connection con = null;
		PreparedStatement pst = null;
		try {
			con = JdbcUtil.getCon();
			System.out.println("传入对象的ID"+user.getUid());
			pst = con.prepareStatement("select upsd,uname,pid,grade from tbl_user where u_id=? ");
			pst.setString(1, user.getUid());	
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				User u =new User();
				u.setGrade(rs.getString("grade"));
				u.setPid(rs.getString("pid"));
				u.setUname(rs.getString("uname"));
				u.setUpsd(rs.getString("upsd"));			
				return u;
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.closeCon(con, pst);
		}
//		System.out.println("传入对象的ID"+user.getUid());
//		String sql = "select uname,upsd,grade,pid from tbl_user where u_id=?";
//		Object[] params = new Object[]{user.getUid()};		
//		RowMapper maprow = new RowMapper<User>(User.class);
//		List<User> userlist = (List<User>) jt.query(sql,params,new mayrowmapper());
//		System.out.println("1thjj");
//		
		return null;
	}

	private class mayrowmapper implements RowMapper<User> {
		@Override
		public User mapRow(ResultSet rs, int arg1) throws SQLException {
			User user = new User();
			user.setUid(rs.getString("u_id"));
			user.setUname(rs.getString("uname"));
			user.setUpsd(rs.getString("upsd"));
			user.setGrade(rs.getString("grade"));
			user.setPid(rs.getString("pid"));
			return user;
		}
	}

	@Override
	public User alter(User user) {
		System.out.println("DID："+user.getUid());
		System.out.println("D名字："+user.getUname());
		  System.out.println("D密码："+user.getUpsd());
		String sql = "UPDATE tbl_user SET pid=?,uname=?,upsd=? WHERE u_id=?";	
		Object[] params = new Object[] { user.getPid(), user.getUname(), user.getUpsd(),user.getUid() };
		int updateRecordNum = jt.update(sql, params);
		if (updateRecordNum == 1)
			return user;
		return null;
	}


	@Override
	public List<User> queryHobbyUser(User u) {
		String sql = "select u_id,uname,pid from tbl_user where u_id in(select u_id from tbl_choose where hid in (select hid from tbl_choose c,tbl_user u where u.u_id=c.u_id and u.u_id= ?));";
		Object[] params = new Object[]{u.getUid()};
		List<User> userlist = (List<User>)jt.query(sql,params,new rowmapper());
		return userlist;
	}
	
	private class rowmapper implements RowMapper<User> {
		@Override
		public User mapRow(ResultSet rs, int arg1) throws SQLException {
			User user = new User();
			user.setUid(rs.getString("u_id"));
			user.setUname(rs.getString("uname"));
			user.setPid(rs.getString("pid"));
			return user;
		}
	}
}
